﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using exsportance.Silverlight.Common.Utils;

namespace exsportance.Silverlight.Validate.Validators
{
    /// <summary>
    /// Validator for fields where a value must be a float
    /// </summary>
    public class FloatValidator : BaseValidator
    {
        #region members
        /// <summary>
        /// Validation failure message key for when the value is not float
        /// </summary>
        private const string NOT_FLOAT = "notFloat";

        /// <summary>
        /// Validation failure message key for when the value is an empty string
        /// </summary>
        private const string STRING_EMPTY = "stringEmpty";

        protected Dictionary<string, string> _messagesTemplates = new Dictionary<string, string>(){
        {FloatValidator.NOT_FLOAT,"'%value%' does not appear to be a float"},
         {FloatValidator.STRING_EMPTY,"'%value%' is an empty string"},
        };
        #endregion

        #region publiprotectedc methods
        protected override bool CheckValid(object value)
        {
            string valueString = value as string;

            rememberValue(valueString);

            if (valueString.IsNullOrEmpty())
            {
                raiseMessage(FloatValidator.STRING_EMPTY);
                return false;
            }

            float floatValue;

            if (!float.TryParse(valueString, out floatValue))
            {
                raiseMessage(FloatValidator.NOT_FLOAT);
                return false;
            }

            return true;
        }

        protected override System.Collections.Generic.Dictionary<string, string> getMessageTemplates()
        {
            return _messagesTemplates;
        }
        #endregion
    }
}
